import 'package:business_plugin/business/adjust_attribution_service.dart';
import 'package:business_plugin/business/admob_service.dart';
import 'package:business_plugin/business/config_business/business_config_model.dart';
import 'package:business_plugin/business/config_business/config_business.dart';
import 'package:business_plugin/business/firebase_helper.dart';
import 'package:business_plugin/business/firebase_remote_config_service.dart';
import 'package:business_plugin/business/report_utils/shared_preferences_tools/shared_preferences_tools.dart';
import 'package:business_plugin/business/td_analytics_service.dart';
import 'package:business_plugin/channel/business_channel_plugin.dart';
import 'package:business_plugin/utils/business_tools.dart';
import 'package:business_plugin_example/my_business_callback.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

Future<void> main() async {
  final config = BusinessConfigModel(
    apiKey: "AIzaSyC9E5tRiHd_kZwJOTo16OGxZ0BYrlQXljo",
    appId: "1:934515110780:ios:757673910b0d1e2bf2eaf9",
    messagingSenderId: "934515110780",
    projectId: "heartbeat-c6717",
    storageBucket: "heartbeat-c6717.firebasestorage.app",
    iosBundleId: "com.cqxm.heartbeat",
    flyOpenId: "ca-app-pub-3392799053230605/4873653387",
    bannerAdId: "ca-app-pub-3392799053230605/1451900237",
    releaseAdvertisement: true,
    tdAppId: "f330873a9228471995fbacfb105bc1b4",
    tdServerUrl: "https://ss.zolnm.com/",
    adjustIsDebug: false,
    adjustId: "bl21rggnyt4w",
    adjustLogLevel: BusinessConfigModel.parseAdjustLogLevel("verbose"),
    rewardedAdId: '',
  );
  // 初始化配置
  ConfigBusiness.initialize(config);
  ConfigBusiness.adjustCallback = (isPromoteUser) {
    BusinessTools.log("是否是买量用户: $isPromoteUser");
  };

  await initAdMob();
  // 确保Flutter框架已经初始化
  WidgetsFlutterBinding.ensureInitialized();
  await SharedPreferencesTools.init();

  BusinessChannelPlugin().configure(
    callback: MyBusinessCallback(),
  );
  initServices();
  runApp(const MyApp());
}

// 初始化AdMob服务
Future<void> initAdMob() async {
  // 初始化AdMob服务并等待完成
  await Get.putAsync<AdMobService>(() async {
    final service = AdMobService();
    return service;
  }, permanent: true);
}

Future<void> initFirebaseHelper() async {
  try {
    await FirebaseHelper.init();
    BusinessTools.log('FirebaseHelper 初始化完成');
  } catch (e) {
    BusinessTools.log('FirebaseHelper 初始化失败: $e');
    //重新初始化
    await initFirebaseHelper();
  }
}

// 初始化所有GetX服务
Future<void> initServices() async {
  await initFirebaseHelper();

  try {
    await Future.wait([
      Get.putAsync<FirebaseRemoteConfigService>(() async {
        final service = FirebaseRemoteConfigService();
        return service;
      }, permanent: true),
      Get.putAsync<AdjustAttributionService>(() async {
        final service = AdjustAttributionService();
        // 关键：等待服务内部初始化完成
        await service.initializeService();
        // service.initializeService();
        return service;
      }, permanent: true),
      Get.putAsync<TDAnalyticsService>(() async {
        final service = TDAnalyticsService();
        await service.initializeService();
        // service.initializeService();
        return service;
      }, permanent: true),
    ]);

    BusinessTools.log('所有服务初始化完成');
  } catch (e) {
    BusinessTools.log('服务初始化失败: $e');
  }
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: const Center(
          child: Text('Running on: \n'),
        ),
      ),
    );
  }
}
